package com.yangguang.huahui.ui.fragments;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.yangguang.huahui.R;
import com.yangguang.huahui.baseadapter.BaseQuickAdapter;
import com.yangguang.huahui.entity.AttentionList;
import com.yangguang.huahui.entity.ClientNeeds;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.entity.Nongzi;
import com.yangguang.huahui.mvp.presenter.CollectionListPresenter;
import com.yangguang.huahui.mvp.view.CollectionListView;
import com.yangguang.huahui.ui.adapters.ClientNeedsAdapter;
import com.yangguang.huahui.ui.adapters.MiaomuAdapter;
import com.yangguang.huahui.ui.adapters.NongziAdapter;
import com.yangguang.huahui.utils.CommonUtil;
import com.yangguang.huahui.utils.GsonUtil;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/4/17.
 */

public class CollectionListFragment extends BaseMvpFragment<CollectionListView, CollectionListPresenter> implements CollectionListView, SwipeRefreshLayout.OnRefreshListener {
    public static CollectionListFragment newInstance(int type) {
        CollectionListFragment fragment = new CollectionListFragment();
        Bundle arguments = new Bundle();
        arguments.putInt("type", type);
        fragment.setArguments(arguments);
        return fragment;
    }

    @BindView(R.id.mSwip)
    SwipeRefreshLayout mSwip;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private LinearLayoutManager layoutManager;
    private BaseQuickAdapter mAdapter;
    @Override
    protected int initLayoutId() {
        return R.layout.frag_collection_list;
    }

    @Override
    protected void initView() {
        mSwip.setColorSchemeResources(R.color.main_color, R.color.main_color, R.color.main_color);
        mSwip.setOnRefreshListener(this);
        layoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(layoutManager);
        //实现首次自动显示加载提示
        mSwip.post(new Runnable() {
            @Override
            public void run() {
                mSwip.setRefreshing(true);
            }
        });

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onSuccess(String result) {
        mSwip.setRefreshing(false);
        switch (getArguments().getInt("type")){
            case 1:
                ClientNeeds clientNeeds = GsonUtil.parseJsonWithGson(result, ClientNeeds.class);
                mAdapter = new ClientNeedsAdapter(R.layout.client_needs_list_item, clientNeeds.getList());
                break;
            case 8:
                AttentionList attentionList = GsonUtil.parseJsonWithGson(result, AttentionList.class);
                mAdapter = new MiaomuAdapter(R.layout.find_miaomu_list_item, attentionList.getList());
                break;
            case 9:
                AttentionList attentionFlowers = GsonUtil.parseJsonWithGson(result, AttentionList.class);
                mAdapter = new MiaomuAdapter(R.layout.find_miaomu_list_item, attentionFlowers.getList());
                break;
            case 10:
                Nongzi nongzi = GsonUtil.parseJsonWithGson(result, Nongzi.class);
                mAdapter = new NongziAdapter(R.layout.nongzi_list_item, nongzi.getList(), 1);
                mRecyclerView.setPadding(CommonUtil.dipToPx(mActivity, 8), CommonUtil.pxToDip(mActivity, 8), CommonUtil.pxToDip(mActivity, 8), 0);
                break;
            case 23:
                Nongzi nongzi1 = GsonUtil.parseJsonWithGson(result, Nongzi.class);
                mAdapter = new NongziAdapter(R.layout.nongzi_list_item, nongzi1.getList());
                mRecyclerView.setPadding(CommonUtil.dipToPx(mActivity, 8), CommonUtil.pxToDip(mActivity, 8), CommonUtil.pxToDip(mActivity, 8), 0);
                break;
        }
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onError() {

    }

    @Override
    protected CollectionListPresenter initPresenter() {
        return new CollectionListPresenter();
    }

    @Override
    protected void fetchData() {
        mPresenter.getCollections(DataInfo.TOKEN, getArguments().getInt("type"));
    }

    @Override
    public void onRefresh() {
        fetchData();
    }
}
